﻿<script lang="ts" name="productCard" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import type { FormRules } from "element-plus";
import { useProductCardApi } from '/@/api/main/productCard';
import { useProductApi } from '/@/api/main/product';

//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const productCardApi = useProductCardApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	dropdownData: {} as any,
});

// 自行添加其他规则
const rules = ref<FormRules>({
	productId: [{ required: true, message: '请选择产品！', trigger: 'blur', },],
	costPrice: [{ required: true, message: '请选择成本价！', trigger: 'blur', },],
	codeRange: [{ required: true, message: '请输入号码段！', trigger: 'blur', },],
	expirationDate: [{ required: true, message: '请选择有效期！', trigger: 'change', },],
});

// 页面加载时
onMounted(async () => {
	state.loading = true;
	var res = await useProductApi().getSelectList(null, false);
	state.dropdownData.productData = res.data.result;
	state.loading = false;
});

// 打开弹窗
const openDialog = async (row: any, title: string) => {
	state.title = title;
	row = row ?? {};
	state.ruleForm = row.id ? await productCardApi.detail(row.id).then(res => res.data.result) : JSON.parse(JSON.stringify(row));
	state.showDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	state.showDialog = false;
};

// 提交
const submit = async () => {
	if(state.loading) {
		return;
	}
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			state.loading = true;
			try {
				let values = state.ruleForm;
				await productCardApi[state.ruleForm.id ? 'update' : 'add'](values);
				closeDialog();
			}
			finally {
				state.loading = false;
			}
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="productCard-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules"
				v-loading="state.loading">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="产品Id" prop="productId">
							<!-- <el-input v-model="state.ruleForm.productId" placeholder="请输入产品Id" maxlength="19" show-word-limit clearable /> -->
							<el-select v-model="state.ruleForm.productId" placeholder="请选择产品" clearable>
								<el-option v-for="item in state.dropdownData.productData" :key="item.id"
									:label="item.name" :value="item.id" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="成本价" prop="costPrice">
							<el-input-number v-model="state.ruleForm.costPrice" placeholder="请输入成本价" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="有效期" prop="expirationDate">
							<el-date-picker v-model="state.ruleForm.expirationDate" type="date" placeholder="有效期" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="号码段" prop="codeRange">
							<el-input type="textarea" height="50" rows="5" v-model="state.ruleForm.codeRange"
								placeholder="请输入ICCID" clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => state.showDialog = false">取 消</el-button>
					<el-button @click="submit" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>